import logger from '@common/logger';
import { getBrandIdCookie } from './info';
import { getInfo, setInfo } from '@common/storage';
import { TipError } from '@common/tip';
// 充值页面方法
// 充值默认数据
const rechargeItemData = [
    { amount: 100, id: 1, giftAmount: 0, couponsText: '' },
    { amount: 200, id: 2, giftAmount: 0, couponsText: '' },
    { amount: 300, id: 3, giftAmount: 0, couponsText: '' },
    { amount: 500, id: 4, giftAmount: 0, couponsText: '' },
    { amount: 1000, id: 5, giftAmount: 0, couponsText: '' },
];

/**
 * 根据充值活动
 * @param {Array} activity 充值活动
 */
export const getRechargeData = (activity=[]) => {
    const len = activity.length;
    if (len === 0) {
        return rechargeItemData;
    }
    let _result = [];
    for (let i = 0; i < len; i++) {
        const item = activity[i];
        _result.push({
            id: i,
            amount: item.quota,
            giftAmount: item.amount,
            couponsText: getRechargeGiveText(item.coupons),
        });
    }
    // 根据充值活动重新设置数据
    return _result;
};

/**
 * 根据充值金额计算最后实际充值的赠送金额，通过活动匹配计算得出
 * @param {Array} activity 充值活动
 * @param {Number} value 充值金额
 */
export const getRechargeGiftAmount = (activity, value) => {
    const len = activity.length;
    if (len === 0) {
        // 没有活动，直接返回
        return 0;
    }
    if (len === 1) {
        // 只有一个充值活动
        return value > activity[0].quota ? activity[0].amount : 0;
    }
    // 多个充值活动
    let result;
    for (let i = 0; i < len; i++) {
        const item = activity[i];
        if (value > item.quota) {
            if (i === len - 1) {
                // 最后一个元素
                result = item.amount;
                break;
            } else {
                // 不是最后一个元素，继续执行循环
                continue;
            }
        } else if (value === item.quota) {
            // 直接返回
            result = item.amount;
            break;
        } else {
            // 取上一个元素的赠送值
            result = i >= 1 ? activity[i - 1].amount : 0;
            break;
        }
    }
    return result;
};

/**
 * 根据优惠券列表，计算出送出的红包和商品劵的总额
 * @param {Array} coupons 优惠券列表
 */
export const getRechargeGiveText = (coupons) => {
    const len = coupons.length;
    if (len === 0) {
        return '';
    }
    let _red_val = 0;
    let _product_val = 0;
    for (let i = 0; i < len; i++) {
        const { couponType, price, cnt } = coupons[i];
        if (couponType === 'voucher') {
            _red_val += price * cnt;
        }
        if (couponType === 'cut' || couponType === 'limit') {
            _product_val += cnt;
        }
    }
    return `送${_product_val ? '商品劵x' + _product_val : ''}${_product_val && _red_val ? '+' : ''}${_red_val ? _red_val.toFixed(2) + '元红包劵' : ''}`;
};

/**
 * 获取充值的参数，判断是否到达起充金额
 * @param {Object} cardInfo 会员卡详情
 * @param {Number} amount 充值金额
 * @param {Number} gift_amount 赠送金额
 */
export const getRechargeParam = (cardInfo, amount, gift_amount=0) => {
    logger.info('选择的会员卡：', cardInfo);
    // 判断是否是到达起充金额
    const { grade } = cardInfo;
    if ((amount - 0) < grade.topUpLowerAmount) {
        TipError(`没达到起充金额￥${grade.topUpLowerAmount}`);
        return false;
    }
    return {
        sg_id: getBrandIdCookie(),
        store_id: '',
        grade_name: cardInfo.gradeName,
        grade_id: cardInfo.gradeId,
        group_id: cardInfo.groupId,
        amount,
        gift_amount,
    };
};
